﻿using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ObjectDB.Library;

namespace ObjectDB.Testing
{
	[TestClass]
	public class PersistenceTests
	{
		public class Name
		{
			public string First { get; set; }
			public string Middle { get; set; }
			public string Last { get; set; }
		}

		[TestMethod]
		public void Can_serialize_simple_object()
		{
			var name = new Name()
			{
				First = "Billy",
				Middle = "Bob",
				Last = "Jones"
			};
			var elem = Persister.Serialize(name);

			Assert.AreEqual("Name", elem.Name);
			Assert.AreEqual(3, elem.Elements().Count());
			Assert.IsNotNull(elem.Element("_lt_First_gt_k__BackingField"));
			Assert.IsNotNull(elem.Element("_lt_Middle_gt_k__BackingField"));
			Assert.IsNotNull(elem.Element("_lt_Last_gt_k__BackingField"));
			Assert.AreEqual(1, elem.Element("_lt_First_gt_k__BackingField").Elements().Count());
			Assert.AreEqual(1, elem.Element("_lt_Middle_gt_k__BackingField").Elements().Count());
			Assert.AreEqual(1, elem.Element("_lt_Last_gt_k__BackingField").Elements().Count());
			Assert.IsNotNull(elem.Element("_lt_First_gt_k__BackingField").Element("String"));
			Assert.IsNotNull(elem.Element("_lt_Middle_gt_k__BackingField").Element("String"));
			Assert.IsNotNull(elem.Element("_lt_Last_gt_k__BackingField").Element("String"));
			Assert.AreEqual("George", elem.Element("_lt_First_gt_k__BackingField").Element("String").Value);
			Assert.AreEqual("Hamilton", elem.Element("_lt_Middle_gt_k__BackingField").Element("String").Value);
			Assert.AreEqual("Tomes", elem.Element("_lt_Last_gt_k__BackingField").Element("String").Value);
		}

		[TestMethod]
		public void Can_deserialize_simple_object()
		{
			var name = new Name()
			{
				First = "Billy",
				Middle = "Bob",
				Last = "Jones"
			};
			var elem = Persister.Serialize(name);

			name = null;
			name = Persister.Deserialize(elem) as Name;
			Assert.AreEqual("George", name.First);
			Assert.AreEqual("Hamilton", name.Middle);
			Assert.AreEqual("Tomes", name.Last);
		}
	}
}